This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
library(tidyverse)
── Attaching core tidyverse packages ────────────────────────────────────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.0.2 ── Conflicts ──────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
Visualizations for 2025 QCBS Poster Symposium.
Map with associated environmental data:
australia_map <- map_data("world", region = "Australia")
ggplot() +
geom_polygon(data = australia_map, aes(x = long, y = lat, group = group),
fill = NA, color = "black", alpha = 0.5) + # Transparent inside
geom_point(data = aus_data, aes(x = long_deg, y = lat_deg), size = 4) +
theme_void() +
theme(
panel.background = element_rect(fill = "transparent", colour = NA),
plot.background = element_rect(fill = "transparent", colour = NA)
)
#looks squished but will work
Species observations on map:
#species frequency table with associated location for plotting
australia_map <- map_data("world", region = "Australia")
species_geo <- tibble(
species_binom = aus_data$species_binom,
lat = aus_data$lat_deg,
long = aus_data$long_deg) %>%
group_by(species_binom) %>%
mutate(frequency = n()) %>%
ungroup() %>%
select(species_binom, lat, long, frequency) %>%
distinct() %>% #to remove duplicate rows
arrange(desc(frequency))
ggplot() +
geom_polygon(data = australia_map, aes(x = long, y = lat, group = group),
fill = "white", color = "black") +
geom_point(data = subset(species_geo, frequency > 50),
aes(x = long, y = lat, color = species_binom), size = 2) +
scale_color_discrete() +
theme_classic()
ggplot() + australia_map +
geom_point(data = subset(species_geo, frequency > 50), mapping = aes(x = long, y = lat,
color = species_binom), size = 2) +
scale_color_discrete() +
theme_classic()
Warning: Incompatible methods ("+.gg", "Ops.data.frame") for "+"Error in ggplot() + australia_map :
non-numeric argument to binary operator
Leaf nutrient concentration by myc_type and fixation:
leaf_concentration_data <- aus_data %>%
select(Unique_ID, woodiness, reclass_life_history, putative_BNF, myc_type,
leaf_N_per_dry_mass, leaf_P_per_dry_mass, leaf_C_per_dry_mass,
NP_ratio, CN_ratio, CP_ratio,
ln_NP_ratio, ln_CN_ratio, ln_CP_ratio) %>%
pivot_longer(cols = c(leaf_N_per_dry_mass, leaf_P_per_dry_mass, leaf_C_per_dry_mass,
NP_ratio, CN_ratio, CP_ratio,
ln_NP_ratio, ln_CN_ratio, ln_CP_ratio),
names_to = "nutrient",
values_to = "concentration") %>%
mutate(nutrient = factor(case_when(
nutrient == "leaf_N_per_dry_mass" ~ "N",
nutrient == "leaf_P_per_dry_mass" ~ "P",
nutrient == "leaf_C_per_dry_mass" ~ "C",
nutrient == "NP_ratio" ~ "N:P Ratio",
nutrient == "CN_ratio" ~ "C:N Ratio",
nutrient == "CP_ratio" ~ "C:P Ratio",
nutrient == "ln_NP_ratio" ~ "Logged N:P Ratio",
nutrient == "ln_CN_ratio" ~ "Logged C:N Ratio",
nutrient == "ln_CP_ratio" ~ "Logged C:P Ratio",
)))
#ggplot(leaf_concentration_data, aes(x = factor(woodiness), y = concentration, fill = nutrient)) +
# geom_boxplot() +
#scale_fill_manual(values = c("N" = "pink", "P" = "lightyellow", "C" = "lightgreen")) +
# facet_wrap(~woodiness, scales = "free") +
#theme_classic()
ggplot(subset(leaf_concentration_data, nutrient == "N"),
aes(x = factor(myc_type), y = concentration)) +
geom_boxplot(fill = "pink") +
#labs(x = "Woodiness", y = "Concentration (per dry mass)",
# title = "Leaf N by Woodiness")
theme_classic()
ggplot(subset(leaf_concentration_data, nutrient == "Logged N:P Ratio"),
aes(x = factor(reclass_life_history), y = concentration)) +
geom_boxplot(fill = "lightgrey") +
#labs(x = "Woodiness", y = "Concentration (per dry mass)",
# title = "Leaf N by Woodiness")
theme_classic()
NA
NA
Leaf nutrient concentration by species dot plot:
pruned <- prune_ausdata(aus_data, 62)
complete <- aus_data %>% mutate(species_binom = "All Species")
combined <- bind_rows(pruned, complete)
# Capitalize species_binom names
capitalize_species <- function(name) {
name_parts <- strsplit(name, "_")[[1]]
paste(toupper(substring(name_parts, 1, 1)), substring(name_parts, 2), sep = "", collapse = " ")
}
# Apply the capitalization function to species names in pruned dataframe
pruned <- pruned %>%
mutate(species_binom = sapply(species_binom, capitalize_species))
# Correctly set the factor levels with "All Species" first
combined <- bind_rows(complete, pruned)
combined$species_binom <- factor(combined$species_binom,
levels = c("All Species", unique(pruned$species_binom)))
#Explicitly define factor levels with "All Species" first
# Plot with adjustments
ggplot(combined, aes(x = species_binom, y = leaf_N_per_dry_mass)) +
geom_jitter(width = 0.2, height = 0, size = 1.5, alpha = 0.6, color = "black") +
theme_classic() +
labs(title = "",
y = "Leaf Nitrogen Concentration (per dry mass)",
x = NULL) +
theme(
axis.text.x = element_text(angle = 50, hjust = 1, size = 20, color = "black"),
axis.text.y = element_text(size = 14, color = "black"),
axis.title.y = element_text(size = 20, color = "black", margin = margin(r = 20)),
plot.title = element_text(size = 16, color = "black")
)
Species-level lin. reg:
pruned <- prune_ausdata(aus_data, 62)
ggplot(pruned, aes(x = SN_total_0_30, y = (leaf_N_per_dry_mass), color = species_binom)) +
geom_point(alpha = 0.6) +
geom_smooth(method = "lm", se = FALSE)
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
List of 136
$ line :List of 6
..$ colour : chr "black"
..$ linewidth : num 0.5
..$ linetype : num 1
..$ lineend : chr "butt"
..$ arrow : logi FALSE
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_line" "element"
$ rect :List of 5
..$ fill : chr "white"
..$ colour : chr "black"
..$ linewidth : num 0.5
..$ linetype : num 1
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_rect" "element"
$ text :List of 11
..$ family : chr ""
..$ face : chr "plain"
..$ colour : chr "black"
..$ size : num 11
..$ hjust : num 0.5
..$ vjust : num 0.5
..$ angle : num 0
..$ lineheight : num 0.9
..$ margin : 'margin' num [1:4] 0points 0points 0points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : logi FALSE
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ title : NULL
$ aspect.ratio : NULL
$ axis.title : NULL
$ axis.title.x :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : num 1
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 2.75points 0points 0points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.title.x.top :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : num 0
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 0points 2.75points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.title.x.bottom : NULL
$ axis.title.y :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : num 1
..$ angle : num 90
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 2.75points 0points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.title.y.left : NULL
$ axis.title.y.right :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : num 1
..$ angle : num -90
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 0points 0points 2.75points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.text :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : chr "grey30"
..$ size : 'rel' num 0.8
..$ hjust : NULL
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : NULL
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.text.x :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : num 1
..$ vjust : num 1
..$ angle : num 45
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 2.2points 0points 0points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi FALSE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.text.x.top :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : num 0
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 0points 2.2points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.text.x.bottom : NULL
$ axis.text.y :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : num 1
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 2.2points 0points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.text.y.left : NULL
$ axis.text.y.right :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : num 0
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 0points 0points 2.2points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.text.theta : NULL
$ axis.text.r :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : num 0.5
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 2.2points 0points 2.2points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.ticks :List of 6
..$ colour : chr "grey20"
..$ linewidth : NULL
..$ linetype : NULL
..$ lineend : NULL
..$ arrow : logi FALSE
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_line" "element"
$ axis.ticks.x : NULL
$ axis.ticks.x.top : NULL
$ axis.ticks.x.bottom : NULL
$ axis.ticks.y : NULL
$ axis.ticks.y.left : NULL
$ axis.ticks.y.right : NULL
$ axis.ticks.theta : NULL
$ axis.ticks.r : NULL
$ axis.minor.ticks.x.top : NULL
$ axis.minor.ticks.x.bottom : NULL
$ axis.minor.ticks.y.left : NULL
$ axis.minor.ticks.y.right : NULL
$ axis.minor.ticks.theta : NULL
$ axis.minor.ticks.r : NULL
$ axis.ticks.length : 'simpleUnit' num 2.75points
..- attr(*, "unit")= int 8
$ axis.ticks.length.x : NULL
$ axis.ticks.length.x.top : NULL
$ axis.ticks.length.x.bottom : NULL
$ axis.ticks.length.y : NULL
$ axis.ticks.length.y.left : NULL
$ axis.ticks.length.y.right : NULL
$ axis.ticks.length.theta : NULL
$ axis.ticks.length.r : NULL
$ axis.minor.ticks.length : 'rel' num 0.75
$ axis.minor.ticks.length.x : NULL
$ axis.minor.ticks.length.x.top : NULL
$ axis.minor.ticks.length.x.bottom: NULL
$ axis.minor.ticks.length.y : NULL
$ axis.minor.ticks.length.y.left : NULL
$ axis.minor.ticks.length.y.right : NULL
$ axis.minor.ticks.length.theta : NULL
$ axis.minor.ticks.length.r : NULL
$ axis.line :List of 6
..$ colour : chr "black"
..$ linewidth : 'rel' num 1
..$ linetype : NULL
..$ lineend : NULL
..$ arrow : logi FALSE
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_line" "element"
$ axis.line.x : NULL
$ axis.line.x.top : NULL
$ axis.line.x.bottom : NULL
$ axis.line.y : NULL
$ axis.line.y.left : NULL
$ axis.line.y.right : NULL
$ axis.line.theta : NULL
$ axis.line.r : NULL
$ legend.background :List of 5
..$ fill : NULL
..$ colour : logi NA
..$ linewidth : NULL
..$ linetype : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_rect" "element"
$ legend.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
..- attr(*, "unit")= int 8
$ legend.spacing : 'simpleUnit' num 11points
..- attr(*, "unit")= int 8
$ legend.spacing.x : NULL
$ legend.spacing.y : NULL
$ legend.key : NULL
$ legend.key.size : 'simpleUnit' num 1.2lines
..- attr(*, "unit")= int 3
$ legend.key.height : NULL
$ legend.key.width : NULL
$ legend.key.spacing : 'simpleUnit' num 5.5points
..- attr(*, "unit")= int 8
$ legend.key.spacing.x : NULL
$ legend.key.spacing.y : NULL
$ legend.frame : NULL
$ legend.ticks : NULL
$ legend.ticks.length : 'rel' num 0.2
$ legend.axis.line : NULL
$ legend.text :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : 'rel' num 0.8
..$ hjust : NULL
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : NULL
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ legend.text.position : NULL
$ legend.title :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : num 0
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : NULL
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ legend.title.position : NULL
$ legend.position : chr "right"
$ legend.position.inside : NULL
$ legend.direction : NULL
$ legend.byrow : NULL
$ legend.justification : chr "center"
$ legend.justification.top : NULL
$ legend.justification.bottom : NULL
$ legend.justification.left : NULL
$ legend.justification.right : NULL
$ legend.justification.inside : NULL
$ legend.location : NULL
$ legend.box : NULL
$ legend.box.just : NULL
$ legend.box.margin : 'margin' num [1:4] 0cm 0cm 0cm 0cm
..- attr(*, "unit")= int 1
$ legend.box.background : list()
..- attr(*, "class")= chr [1:2] "element_blank" "element"
$ legend.box.spacing : 'simpleUnit' num 11points
..- attr(*, "unit")= int 8
[list output truncated]
- attr(*, "class")= chr [1:2] "theme" "gg"
- attr(*, "complete")= logi TRUE
- attr(*, "validate")= logi TRUE
Missing data densities:
na_data <- aus_data %>%
mutate(across(c(leaf_P_per_dry_mass,leaf_C_per_dry_mass,
NP_ratio, CN_ratio, CP_ratio), as.character)) %>%
replace_na(list(leaf_N_per_dry_mass= "NA", leaf_P_per_dry_mass = "NA",
leaf_C_per_dry_mass = "NA",NP_ratio = "NA",
CN_ratio = "NA", CP_ratio = "NA"))
na_data <- na_data %>%
filter(leaf_P_per_dry_mass == "NA" | leaf_C_per_dry_mass == "NA" |
NP_ratio == "NA" | CN_ratio == "NA" | CP_ratio == "NA")
#specify colors
color_palette <- c("NA rows" = "lightgray", "All data" = "lightgreen")
#density plot of missing leaf concentration data
#using (complete) leaf N to compare rows with NAs for P and C
ggplot() +
geom_density(data = aus_data,
mapping = aes(x = leaf_N_per_dry_mass,
fill = "All data", alpha = 0.5)) +
geom_density(data = na_data,
mapping = aes(x = leaf_N_per_dry_mass,
fill = "NA rows", alpha = 0.5)) +
labs(title = "Leaf N observations with Missing Leaf P and/or Leaf C",
x = "leaf N", y = "Density") +
theme_classic() +
scale_fill_manual(values = color_palette, name = " ") +
theme(legend.position = c(0.83, 0.86))
#missing env. coverage
ggplot() +
geom_density(data = aus_data,
mapping = aes(x = SN_total_0_30,
fill = "All data", alpha = 0.5)) +
geom_density(data = na_data,
mapping = aes(x = SN_total_0_30,
fill = "NA rows", alpha = 0.5)) +
labs(title = "Observations with Missing Leaf P and/or Leaf C",
y = "Density") +
theme_classic() +
scale_fill_manual(values = color_palette, name = " ") +
theme(legend.position = c(0.83, 0.86))
Phylogenetic tree:
library(ggtree)
library(tidytree)
library(treeio)
setwd("/Users/sofiaquijada/Library/Mobile Documents/com~apple~CloudDocs/McGill/2024 Soper Lab/AusStoich-Collab")
Warning: The working directory was changed to /Users/sofiaquijada/Library/Mobile Documents/com~apple~CloudDocs/McGill/2024 Soper Lab/AusStoich-Collab inside a notebook chunk. The working directory will be reset when the chunk is finished running. Use the knitr root.dir option in the setup chunk to change the working directory for notebook chunks.
#works but only if all run at once
ausdata_all_pos_sp_tree <- read.tree("Inputs/Trees/ausdata_all_pos_sp.tre")
austraits_all_pos_sp_df <- read_csv('Inputs/all_pos_austraits_LCVP_sp.csv')
Rows: 829 Columns: 5── Column specification ─────────────────────────────────────────────────────────────────
Delimiter: ","
chr (3): species, genus, family
lgl (2): species.relative, genus.relative
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
all_pos_sp_data <- aus_data[aus_data$species_binom %in%
austraits_all_pos_sp_df$species, ]
all_pos_sp_data <- add_CV_columns(select_relevant_columns(all_pos_sp_data))
avg_all_pos_sp_data <- average_nutrient_data(all_pos_sp_data)
#horizontal base
all_pos_sp_plot <- ggtree(ausdata_all_pos_sp_tree) + geom_tiplab(size = 0.5)
#most basic, no coloring, horizontal bar plot
all_pos_sp_plot + geom_facet(
panel = 'Trait',
data = avg_all_pos_sp_data,
geom = geom_col,
mapping = aes(x = avg_leaf_N),
orientation = "y") +
ggtitle("") +
theme(plot.title = element_text(size = 20))
names(avg_all_pos_sp_data)[1] <- "label"
attemptree <- full_join(as.treedata(ausdata_all_pos_sp_tree), avg_all_pos_sp_data, by = "label")
#mess with line thickness, and very small tip labels to manually write over later
#get label in the middle
ggtree(attemptree, aes(color = avg_leaf_N), layout = "circular") +
scale_color_continuous(low = "#6ad1f3", high = "#ee6b00") +
geom_tiplab(size = 0.5)